﻿using System;
using System.Windows.Forms;
using LoaMen.Controls;
using ProxyHero.Common;
using ProxyHero.Setting;

namespace ProxyHero.TabPages
{
    public partial class frmIEBrowser : Form
    {
        #region Variable
        Crownwood.Magic.Controls.TabPage ParentPage;
        #endregion

        #region Constructors
        public frmIEBrowser(string url,Crownwood.Magic.Controls.TabPage parentPage)
        {
            try
            {
                InitializeComponent();
                wbMain.CanGoBackChanged += new EventHandler(wbMain_CanGoBackChanged);
                wbMain.CanGoForwardChanged += new EventHandler(wbMain_CanGoForwardChanged);
                wbMain.StatusTextChanged += new EventHandler(wbMain_StatusTextChanged);
                this.txtUrl.Text = url;
                Config.MainForm.tsslStatus.Text = url;
                this.wbMain.Navigate(url);
                ParentPage = parentPage;
                tsbBack.Enabled = wbMain.CanGoBack;
                tsbForward.Enabled = wbMain.CanGoForward;
            }
            catch (Exception ex)
            {
                MsgBox.ShowExceptionMessage(ex);
            }
        }
        #endregion

        #region Operation
        private void txtUrl_Click(object sender, EventArgs e)
        {
            txtUrl.SelectAll();
        }
        private void txtUrl_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Navigate(txtUrl.Text);
            }
        }
        private void btn_Go_Click(object sender, EventArgs e)
        {
            Navigate(this.txtUrl.Text);
        }
        private void wbMain_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            try
            {
                Config.MainForm.tsslStatus.Text = "完成";
                ParentPage.Title = FormatString(this.wbMain.Document.Title);
            }
            catch (Exception ex)
            {
                Config.MainForm.tsslStatus.Text = ex.Message;
            }
        }
        private void backFileToolStripButton_Click(object sender, EventArgs e)
        {
            wbMain.GoBack();
        }
        private void wbMain_CanGoBackChanged(object sender, EventArgs e)
        {
            tsbBack.Enabled = wbMain.CanGoBack;
        }
        private void forwardFileToolStripButton_Click(object sender, EventArgs e)
        {
            wbMain.GoForward();
        }
        private void wbMain_CanGoForwardChanged(object sender, EventArgs e)
        {
            tsbForward.Enabled = wbMain.CanGoForward;
        }
        private void wbMain_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {
            if (this.wbMain.ProxyServer != "" && !this.wbMain.ProxyLable.Visible)
            {
                this.wbMain.ProxyLable.Visible = true;
            }
            if (!this.wbMain.DocumentText.Contains("LoaMen.Com提示") && !this.wbMain.DocumentText.Contains("<HTML></HTML>"))
            {
                txtUrl.Text = wbMain.Url.ToString();
            }
        }
        private void wbMain_StatusTextChanged(object sender, EventArgs e)
        {
            Config.MainForm.tsslStatus.Text = wbMain.StatusText;
        }
        private void wbMain_BeforeNewWindow(object sender, EventArgs e)
        {
            WebBrowserExtendedNavigatingEventArgs eventArgs = e as WebBrowserExtendedNavigatingEventArgs;
            Config.CreateNewIEBrowser(eventArgs.Url);
            eventArgs.Cancel = true;
        }

        private void wbMain_NavigateError(object sender, WebBrowserNavigateErrorEventArgs e)
        {
            int code = e.StatusCode;
            // 发生错误时，转向本地页面
            if (code == 404)
            {
                this.wbMain.Url = new Uri("http://www.01kk.net/404/html");
            }
            else if (code >= 400 && code <= 505)
            {
                this.wbMain.DocumentText = Config.GetErrorHtml("HTTP错误" + code, "HTTP错误" + code);
            }
            else if (code == -2146697211)
            {
                this.wbMain.DocumentText = Config.GetErrorHtml("HTTP错误" + code, "可能未连接Internet！");
            }
        }
        #endregion

        #region Methods
        private void Navigate(String address)
        {
            if (String.IsNullOrEmpty(address)) return;
            if (address.Equals("about:blank")) return;
            if (!address.StartsWith("http://") &&
                !address.StartsWith("https://"))
            {
                address = "http://" + address;
            }
            try
            {
                wbMain.Navigate(new Uri(address));
            }
            catch (System.UriFormatException)
            {
                return;
            }
        }

        protected string FormatString(string str)
        {
            if (str.Length > 20)
            {
                str = str.Substring(0, 19) + "...";
            }
            else if (str.Length == 0)
            {
                str = "新建标签";
            }
            return str;
        }
        #endregion

        private void tsbFresh_Click(object sender, EventArgs e)
        {
            try
            {
                wbMain.Refresh();
            }
            catch (Exception ex)
            {
                MsgBox.ShowExceptionMessage(ex);
            }
        }

        private void tsbStop_Click(object sender, EventArgs e)
        {
            try
            {
                wbMain.Stop();
            }
            catch (Exception ex)
            {
                MsgBox.ShowExceptionMessage(ex);
            }
        }

        private void tsbSetting_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.txtUrl.Enabled)
                {
                    frmProxySetting formSetting = new frmProxySetting();
                    formSetting.ShowDialog();
                }
            }
            catch (Exception ex)
            {
                MsgBox.ShowExceptionMessage(ex);
            }
        }

        private void tsbHome_Click(object sender, EventArgs e)
        {
            try
            {
                this.wbMain.Navigate("http://www.loamen.com");
            }
            catch (Exception ex)
            {
                MsgBox.ShowExceptionMessage(ex);
            }
        }

        private void tsbFreshPage_Click(object sender, EventArgs e)
        {
            try
            {
                if (Config.ProxyViewForm.tsbAutoChangeProxy.Text == "关闭自动切换代理")
                {
                    if (this.txtUrl.Text != "" && this.txtUrl.Text.ToLower() != "about:blank")
                    {
                        this.txtUrl.Enabled = !this.txtUrl.Enabled;
                        if (this.txtUrl.Enabled)
                        {
                            this.tsbFreshPage.Image = Properties.Resources.lock1;
                        }
                        else
                        {
                            this.tsbFreshPage.Image = Properties.Resources.lock2;
                        }
                    }
                    else
                    {
                        MsgBox.ShowMessage("请输入要刷新的网址！");
                    }
                }
                else
                {
                    MsgBox.ShowMessage("未开启自动切换！");
                }
            }
            catch (Exception ex)
            {
                MsgBox.ShowExceptionMessage(ex);
            }
        }

        private void wbMain_AfterProxyChanged(object sender, EventArgs e)
        {
            try
            {
                if (!this.txtUrl.Enabled)
                {
                    this.wbMain.Navigate(this.txtUrl.Text.Trim());
                }
            }
            catch (Exception ex)
            {
                MsgBox.ShowExceptionMessage(ex);
            }
        }
    }
}
